package com.cucn.staservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cucn.commonutils.Result;
import com.cucn.staservice.client.UcenterClient;
import com.cucn.staservice.entity.StatisticsDaily;
import com.cucn.staservice.mapper.StatisticsDailyMapper;
import com.cucn.staservice.service.StatisticsDailyService;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author KouChaoJie
 * @since 2021-10-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 统计某一天的注册人数,生成统计数据
     *
     * @param day 日期
     */
    @Override
    public void registerCount(String day) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        //远程调用得到某一天注册人数
        Result registerResult = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerResult.getData().get("countRegister");

        //把获取的数据添加到统计分析数据表中
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);
        //统计日期
        sta.setDateCalculated(day);
        //视频播放数
        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));
        //登录数
        sta.setLoginNum(RandomUtils.nextInt(100, 200));
        //新增课程数
        sta.setCourseNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(sta);
    }

    /**
     * 图表显示,返回两部分数据,日期json数组,数量json数组
     *
     * @param type  统计种类
     * @param begin 统计开始时间
     * @param end   统计结束时间
     * @return param map集合
     */
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        //查询从开始到结束事件的数据
        wrapper.between("date_calculated", begin, end);
        //指定要查的列
        wrapper.select("date_calculated", type);

        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //因为返回的有两部分数据,有日期,和日期对应的数量
        //前端要求数据json数组结构,对应后端java代码时list集合
        //创建里两个list集合,一个日期list,一个数量list
        List<String> dateCalculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合,进行封装
        for (StatisticsDaily daily : staList) {
            //封装list集合
            dateCalculatedList.add(daily.getDateCalculated());
            //封装对应的集合
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("date_CalculatedList", dateCalculatedList);
        map.put("numDataList", numDataList);

        return map;
    }
}
